package com.usian.service.impl;

import com.usian.service.TongYiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.dialogue.form.MessageForm;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.StreamingChatClient;
import org.springframework.ai.image.ImageClient;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zqtao
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class TongYiServiceImpl implements TongYiService {

    /**
     * 自动注入ChatClient、imageClient、StreamingChatClient，屏蔽模型调用细节
     */
    private final ChatClient chatClient;

    private final ImageClient imageClient;

    private final StreamingChatClient streamingChatClient;

    @Autowired
    public TongYiServiceImpl(ChatClient chatClient, StreamingChatClient streamingChatClient, ImageClient imageClient) {
        this.chatClient = chatClient;
        this.imageClient = imageClient;
        this.streamingChatClient = streamingChatClient;
    }

    @Override
    public ImageResponse genImg(List<MessageForm> messageForms) {
        if (messageForms == null || messageForms.isEmpty()) {
            throw new IllegalArgumentException("messageForms must not be empty");
        }

        try {
            var prompt = new ImagePrompt(messageForms.get(messageForms.size() - 1).getContent());
            return imageClient.call(prompt);
        } catch (Exception e) {
            // 记录日志并抛出自定义异常或返回默认值
            log.error("Error calling imageClient: ", e);
            throw new RuntimeException("Failed to generate image", e);
        }
    }



}
